﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class ReturnToFactoryConfirmProvider : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            ProviderService providerservice = new ProviderService();
            providerDropDownList.DataSource = providerservice.GetAllProviderWithSelectOne();
            providerDropDownList.DataTextField = "ProviderName";
            providerDropDownList.DataValueField = "ProviderCode";
            providerDropDownList.DataBind();

            runningNumberLabel.Text = form.RunningNumber;
            rtfDateLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            requireLabel.Text = form.Requirement;
            engineerLbl.Text = form.ApplicationProduct.User.ContactPerson;
            Provider provider = providerservice.GetProviderBySerialNOAndVersion(form.ApplicationProduct.ProductSerialNumber, form.ApplicationProduct.ProductVersionNumber);
            if (provider != null)
            {
                providerLbl.Text = provider.ProviderName;
            }
            else
            {
                providerLbl.Text = "找不到相关的厂商信息";
            }
            applyTimeLabel.Text = form.ApplyDate.Value.ToString("yyyy-MM-dd");
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (providerDropDownList.SelectedIndex > 0)
                {
                    // 状态更新
                    ReturnToFactoryService service = new ReturnToFactoryService();
                    ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                    form.Status = "返厂-RMA#申请";
                    form.ProvideCode = providerDropDownList.SelectedValue;
                    form.ConfirmProviderDate = DateTime.Now;
                    service.Update(form);

                    // 邮件通知
                    ProviderService providerservice = new ProviderService();
                    Provider provider = providerservice.GetProviderByCode(providerDropDownList.SelectedValue);
                    string emailBody = WebConfigurationManager.AppSettings["ReturnToFactoryEmail"];
                    if (!string.IsNullOrEmpty(provider.Contact1Email))
                    {
                        EmailService.SendEmail(provider.Contact1Email, "有新的返厂产品", emailBody);
                    }
                    if (!string.IsNullOrEmpty(provider.Contact2Email))
                    {
                        EmailService.SendEmail(provider.Contact2Email, "有新的返厂产品", emailBody);
                    }

                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('保存成功');location.href='ReturnToFactoryListConfirmProvider.aspx';", true);
                }
                else
                {
                    Response.Write(string.Format("<script language='JavaScript'> alert( '请选择返厂厂商！'); </script>"));
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactoryConfirmProvider.aspx::saveLinkButton_Click");
                throw ex;
            }
        }
    }
}
